//
//  TouchImageView.m
//  NLine
//
//  Created by Xiaoyong Li on 12-8-14.
//  Copyright (c) 2012年 sunrise. All rights reserved.
//

#import "TouchImageView.h"
#import "DLog.h"
static const CFTimeInterval kLongPressMinimumDurationSeconds = 0.3;

@interface TouchImageView ()
{
    int mTouchFlag; //默认0，编辑模式下为1
}
@end

@implementation TouchImageView
@synthesize delegate;

-(id) init
{
    self = [super init];
    if(self)
    {
        self.userInteractionEnabled = YES;
        mTouchFlag = 0;
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.userInteractionEnabled = YES;
        mTouchFlag = 0;
    }
    return self;
}
-(id)initWithImage:(UIImage *)image
{
    self = [super initWithImage:image];
    if (self)
    {
        self.userInteractionEnabled = YES;
        mTouchFlag = 0;
    }
    return self;
}
-(id)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage
{
    self = [super initWithImage:image highlightedImage:highlightedImage];
    if (self)
    {
        self.userInteractionEnabled = YES;
        mTouchFlag = 0;
    }
    return self;
}

-(void)dealloc
{
    DEnter();
    [super dealloc];
}
- (void) initialize
{
//    UILongPressGestureRecognizer *recognizer =
//    [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
//    [recognizer setMinimumPressDuration:kLongPressMinimumDurationSeconds];
//    [self addGestureRecognizer:recognizer];
//    [recognizer release];

    mTouchFlag = 1;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (mTouchFlag == 0)
    {
        if (self.delegate)
        {
            [self.delegate clickImageView:self];
        }
    }
    else
    {
        [self handleLongPress];
    }
}

/*
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.delegate)
    {
        [self.delegate clickImageView:self];
    }
}
 */

-(BOOL) canBecomeFirstResponder
{
    return YES;
}

- (void) menuWillShow:(NSNotification *)notification
{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIMenuControllerWillShowMenuNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(menuWillHide:)
                                                 name:UIMenuControllerWillHideMenuNotification
                                               object:nil];
}

-(void)viewListing:(id)sender
{
    DLog();
    if(delegate)
    {
        [delegate viewListing:self];
    }
}
-(void)editListing:(id)sender
{
    DLog();
    if(delegate)
    {
        [delegate editListing:self];
    }
}

#pragma mark -
#pragma mark UILongPressGestureRecognizer Handler Methods

- (void) handleLongPress
{
    [self becomeFirstResponder];
    
    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"设为头像" action:@selector(viewListing:)];
    UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:@"删除照片" action:@selector(editListing:)];
    
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    menuController.menuItems = [NSArray arrayWithObjects:menuItem, menuItem1, nil];
    [menuController setTargetRect:self.bounds inView:self];
    
    /*
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(menuWillShow:)
                                                 name:UIMenuControllerWillShowMenuNotification
                                               object:nil];
     */
    [menuController setMenuVisible:YES animated:YES];
}

@end